package com.sunreal.paygateway.sys.util.result;

import lombok.Data;

import com.sunreal.paygateway.sys.exception.BusinessException;
import com.sunreal.paygateway.sys.exception.WebErrCode;

/**
 * 功能描述：//TODO
 *
 * @Author 彭广
 * @Date 2019/4/18
 */
@Data
public class RetResult {
    private String msg;
    private String sign;
    private Boolean success = false;
    private int errorCode;
    private Object data;

    public static WebResult retSuccess(String msg) {
        WebResult result = new WebResult();
        result.isOK();
        result.setMsg(msg);
        return result;
    }

    public static WebResult retSuccess(String msg, Object data) {
        WebResult result = new WebResult();
        result.isOK();
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static WebResult retFail(int errorCode, String msg) {
        WebResult result = new WebResult();
        result.isWrong();
        result.setMsg(msg);
        result.setErrorCode(errorCode);
        return result;
    }

    public static WebResult retFail(WebErrCode webErrCode) {
        WebResult result = new WebResult();
        result.isWrong();
        result.setMsg(webErrCode.getErroMsg());
        result.setErrorCode(webErrCode.getErroCode());
        return result;
    }

    public static WebResult retFail(Exception e) {
        WebResult result = new WebResult();
        if (e instanceof BusinessException) {
            result.setErrorCode(((BusinessException) e).getErrCode());
            result.setMsg(((BusinessException) e).getErrMsg());
        } else {
            result.setMsg(e.getMessage());
        }
        result.isWrong();
        return result;
    }

    /**
     * 自动设置success=false
     * <br />errorCode = ? 如果是BusinessException，会自动设置
     * <br />msg = e.getMessage()
     *
     * @param e
     */
    public void setException(Exception e) {
        if (e instanceof BusinessException) {
            this.errorCode = ((BusinessException) e).getErrCode();
        }
        this.success = false;
        this.msg = e.getMessage();
    }

    public void isOK() {
        this.success = true;
    }

    public void isWrong() {
        this.success = false;
    }
}
